import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'
import Workbench from '@/components/Workbench.vue'
import Bulid from '@/components/Bulid.vue'
import AddCommunity from '@/components/AddCommunity.vue'  // 假设你有一个AddCommunity组件
import LoginPhone from '@/components/LoginPhone.vue'
import LoginSwitcher from '@/components/LoginSwitcher.vue'
import Desktop from '@/components/Desktop.vue'
import faceLogin from '../views/faceLogin/index'
import Register from '@/components/Register.vue'
import Face from '@/components/Face.vue'
import LicensePlateRecognition from '@/views/LicensePlateRecognition.vue'
import User from '@/components/User.vue'
import Counseling from '@/components/Counseling.vue'
import Framework from '@/components/Framework.vue'
import PayMent from '@/components/PayMent.vue'
import Security from '@/components/Security.vue'
import Intelligent from '@/components/Intelligent.vue'
import record from '@/components/record.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: LoginSwitcher,
  },
  {path:'/bulid',component:Bulid},
  {path:'/workbench',component:Workbench},
  {path:'/add-community',component:AddCommunity},
  {path:'/login',component:Login},
  {path:'/register',component:Register},
  {path:'/login-phone',component:LoginPhone},
  {path:'/login-switcher',component:LoginSwitcher},
  {path:'/faceLogin',name:'faceLogin',component:faceLogin},
  {path:'/licensePlateRecognition',component:LicensePlateRecognition},
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/index'),
    redirect: '/desktop',
    children:[
      {
        path: '/desktop',
        component: Desktop
      },
      {
        path: '/faceList',
        name: 'FaceList',
        component: Face
      },
    ]
  },
  {path:'/user',component:User},
  {path:'/counseling',component:Counseling},
  {path:'/framework',component:Framework},
  {path:'/payMent',component:PayMent},
  {path:'/security',component:Security},
  {path:'/intelligent',component:Intelligent},
  {path:'/record',component:record},


]

const router = new VueRouter({
  routes
})
// 修改导航守卫逻辑
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 如果是访问登录页面
  if (to.path === '/') {
    if (token) {
      // 已登录则跳转到工作台
      next('/workbench')
    } else {
      // 未登录则允许访问登录页
      next()
    }
    return
  }
  
  // 非登录页面
  if (!token) {
    // 未登录则跳转到登录页
    next('/')
  } else {
    // 已登录则允许访问
    next()
  }
})

export default router
